package pers.qianyu.util;

import java.text.*;
import java.util.*;
import java.util.stream.*;

/**
 * @author mizzle rain
 * @date 2020-11-15 22:55
 */
public class ArrayUtil {
    /**
     * 打印数组，主要用于leetcode代码调试
     *
     * @param arr   要打印的数组
     * @param left  数组左指针
     * @param right 数组右指针
     */
    public static void printArr(int[] arr, Integer left, Integer right) {
        if (arr == null || arr.length == 0) {
            return;
        }
        StringBuffer sb = new StringBuffer();
        sb.append("{");
        for (int i = 0; i < arr.length; i++) {
            if (left != null && left.equals(i) || right != null && right.equals(i)) {
                sb.append("[").append(arr[i]).append("]");
            } else {
                sb.append(" ").append(arr[i]).append(" ");
            }
        }
        sb.append("}");
        System.out.println(sb);
    }

    public static void printArr(char[] arr, Integer left, Integer right) {
        if (arr == null || arr.length == 0) {
            return;
        }
        StringBuffer sb = new StringBuffer();
        sb.append("{");
        for (int i = 0; i < arr.length; i++) {
            if (left != null && left.equals(i) || right != null && right.equals(i)) {
                sb.append("[").append(arr[i]).append("]");
            } else {
                sb.append(" ").append(arr[i]).append(" ");
            }
        }
        sb.append("}");
        System.out.println(sb);
    }

    private static boolean isFirst = true;

    public static void printMarkdownTableHead(int len) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < len; i++) {
            if (i != 0 && i != len - 1) {
                builder.append(i).append(" | ");
            } else if (i == len - 1) {
                builder.append(i).append(" |");
            } else if (i == 0) {
                builder.append("|  | ").append(i).append(" | ");
            }
        }
        builder.append("\n");
        for (int i = 0; i < len; i++) {
            if (i != 0 && i != len - 1) {
                builder.append("--- | ");
            } else if (i == len - 1) {
                builder.append("--- |");
            } else if (i == 0) {
                builder.append("| :---: | --- | ");
            }
        }
        System.out.println(builder);
    }

    public static void printArrayAsMarkdownTable(Object first, int[] arr) {
        if (isFirst) {
            printMarkdownTableHead(arr.length);
            isFirst = false;
        }
        if (arr == null || arr.length == 0) {
            return;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            if (i != 0 && i != arr.length - 1) {
                sb.append(arr[i]).append(" | ");
            } else if (i == arr.length - 1) {
                sb.append(arr[i]).append(" |");
            } else if (i == 0) {
                if (first == null) {
                    sb.append("|  | ").append(arr[i]).append(" | ");
                } else {
                    sb.append("| ").append(first.toString()).append(" | ")
                            .append(arr[i]).append(" | ");
                }
            }
        }
        System.out.println(sb);
    }

    public static void printArrayAsMarkdownTable(Object first, double[] arr, int round) {
        if (isFirst) {
            printMarkdownTableHead(arr.length);
            isFirst = false;
        }
        if (arr == null || arr.length == 0) {
            return;
        }
        String[] newArray = convertToStringArray(arr, round);
        printArrayAsMarkdownTableCore(first, newArray);
    }

    private static void printArrayAsMarkdownTableCore(Object first, String[] arr) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            if (i != 0 && i != arr.length - 1) {
                sb.append(arr[i]).append(" | ");
            } else if (i == arr.length - 1) {
                sb.append(arr[i]).append(" |");
            } else if (i == 0) {
                if (first == null) {
                    sb.append("|  | ").append(arr[i]).append(" | ");
                } else {
                    sb.append("| ").append(first.toString()).append(" | ")
                            .append(arr[i]).append(" | ");
                }
            }
        }
        System.out.println(sb);
    }

    public static void printArrayAsMarkdownTable(Object first, boolean[] arr) {
        if (isFirst) {
            printMarkdownTableHead(arr.length);
            isFirst = false;
        }
        if (arr == null || arr.length == 0) {
            return;
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            if (i != 0 && i != arr.length - 1) {
                sb.append(arr[i]).append(" | ");
            } else if (i == arr.length - 1) {
                sb.append(arr[i]).append(" |");
            } else if (i == 0) {
                if (first == null) {
                    sb.append("|  | ").append(arr[i]).append(" | ");
                } else {
                    sb.append("| ").append(first.toString()).append(" | ")
                            .append(arr[i]).append(" | ");
                }
            }
        }
        System.out.println(sb);
    }

    public static void printDeepArrayAsMarkdownTable(int[][] arr) {
        int index = 0;
        for (int[] ints : arr) {
            printArrayAsMarkdownTable(index++, ints);
        }
    }

    private static String[] convertToStringArray(double[] arr, int round) {
        String[] res = new String[arr.length];
        for (int i = 0; i < arr.length; i++) {
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(round);
            res[i] = numberFormat.format(arr[i]);
        }
        return res;
    }


    public static void printIndex(int length) {
        StringBuilder sb = new StringBuilder();
        sb.append(" ");
        for (int i = 0; i < length; i++) {
            sb.append(String.format(" %s ", i));
        }
        System.out.println(sb);
    }

    public static String deepToString(String[][] arr) {
        return Arrays.stream(arr)
                .map(Arrays::toString)
                .map(i -> i + "\n")
                .collect(Collectors.joining());
    }

    public static void printDeepToString(int[][] arr) {
        String[][] strs = convertToStringArray(arr);
        System.out.println(deepToString(strs));
    }

    public static void printDeepToString(char[][] arr) {
        String[][] strs = convertToStringArray(arr);
        System.out.println(deepToString(strs));
    }

    public static void printDeepToString(boolean[][] arr) {
        String[][] strs = convertToStringArray(arr);
        System.out.println(deepToString(strs));
    }

    private static String[][] convertToStringArray(boolean[][] arr) {
        int h = arr.length;
        int w = arr[0].length;
        String[][] res = new String[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                res[i][j] = arr[i][j] + "";
            }
        }
        return res;
    }

    private static String[][] convertToStringArray(char[][] arr) {
        int h = arr.length;
        int w = arr[0].length;
        String[][] res = new String[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                res[i][j] = arr[i][j] + "";
            }
        }
        return res;
    }

    private static String[][] convertToStringArray(int[][] arr) {
        int h = arr.length;
        int w = arr[0].length;
        String[][] res = new String[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                res[i][j] = arr[i][j] + "";
            }
        }
        return res;
    }

    public static int[] subArray(int[] arr, int left, int right) {
        int[] ans = new int[right - left];
        for (int i = left, t = 0; i < right; i++, t++) {
            ans[t] = arr[i];
        }
        return ans;
    }
}
